﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChgDoc.Render;

namespace ChgDoc.Edit.PageRenderControls
{
    public partial class TemplateData : PageRenderControl
    {
        public override string GetValueString()
        {
            TemplateDataRenderParameters param = new TemplateDataRenderParameters();
        	param.TemplateId = int.Parse( drpTemplates.SelectedValue );
            param.Table = drpTable.SelectedValue;
            param.Id = txtId.Text;
            param.CacheDurationSeconds = long.Parse(txtCache.Text);

            return param.Serialize();
        }

        public override void SetValueString(string val)
        {
        	TemplateDataRenderParameters param = TemplateDataRenderParameters.Deserialize( val );
			
			drpTemplates.SelectedValue = param.TemplateId.ToString();
            drpTable.SelectedValue = param.Table;

            txtId.Text = param.Id;
            txtCache.Text = param.CacheDurationSeconds.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            drpTemplates.DataSource = from t in ChgDoc.Entity.Template.ReadAll() select new { t.Id, t.Name };
        	drpTemplates.DataTextField = "Name";
        	drpTemplates.DataValueField = "Id";
            drpTemplates.DataBind();

            drpTable.DataSource = from s in SqliteORM.DbConnection.TableNames(false) select new { Name = s };
            drpTable.DataTextField = "Name";
            drpTable.DataValueField = "Name";
            drpTable.DataBind();

        }
    }
}